# -*- coding: utf-8 -*-
from utils.config import *
from utils.utils import *
import pandas as pd
import utils.MyTT as Mytt
# import logging
from utils.log import Loggers
from utils.pushService import PushService
import numpy as np

# 每小时的前5分钟 不调用脚本，防止数据不完整
should_skip()
logger = Loggers('RsiDaySignal')
# logger.clear()
# 通过使用 rsi 25 金叉死叉 rsi 150 来判断信号。
# 同时，只有价格处于ema120 之下的金叉 才视为有效

pushSer = PushService('RSI日线级别策略')


firstOne = 0
# 循环处理
for instId in config_movingAverage_instId:
    # 读取300条最新的数据
    try:
        df = loadCsvData(instId,'1d')
    except:
        continue
    


    df['rsi25'] = Mytt.RSI(df['close'],25)
    df['rsi150'] = Mytt.RSI(df['close'],150)
    df['ema120'] = Mytt.EMA(df['close'],120)
    df['cclose'] = df['close'].shift(1)

    df['cross_rsi'] = Mytt.CROSS(df['rsi25'],df['rsi150'])

    df['signal'] = 0
    df.loc[(df['close'].shift(1)  < df['ema120']) & (df['cross_rsi'] ==1) ,'signal'] = 1 # 做多信号距离越远 信号可信度越高
    df.loc[(df['close'].shift(1)  > df['ema120']) & (df['cross_rsi'] == -1) ,'signal'] = -1 # 做多信号距离越远 信号可信度越高

    
    # 快速计算信号近期胜率
    # shenglv5,shenglv10 = getShenglv(df)
    # 取出所有满足条件的信号数据
    df_signal = df[df['signal']!=0]
    # print(df.tail(30))
    print(df_signal.tail(30))
    print("数量：",len(df_signal))
    exit()
    # 拿到最后出现信号的数据
    after_signal2 = df_signal.iloc[-2]
    after_signal = df_signal.iloc[-1]

    xiaoshu = 1 if after_signal['close']>0 else 3
    # 计算止损点位
    # zhisun= round(after_signal['recent_low'] + (after_signal['atr'] if after_signal['signal'] == -1 else after_signal['atr']*-1),xiaoshu)

    fangxiang = '做多' if after_signal['signal']>0 else '做空'
    signal_text = f"{instId}{fangxiang} 最近信号：{after_signal.name} 收盘价：{after_signal['close']} "

    # print(after_signal2['close'])
    # exit()
    fangxiang2 = '做多' if after_signal2['signal']>0 else '做空'
    diezhang = round(( after_signal['close'] - after_signal2['close']) / after_signal2['close'] * 100,2)
    signal_text2 = f"上次信号：{after_signal2.name}({fangxiang2}) 距今跌涨：{diezhang}%"
    
    # signal_text+= f"止损：{zhisun}({zhisunR}%)"
    hour = getHoursPassed(after_signal.name)
    # 进行日志记录
    if hour < 50:
        if firstOne == 0:
            firstOne=1
            extent = [
                signal_text2,
            # f"止损：{zhisun}({zhisunR}%)\n\n",
            "温馨提示：\n\n",
            "该信号是大方向信号，通常意味着多空变盘，尽可能的顺势而为\n\n",
            "历史信号出现次数较少，胜率偏高\n\n",
        ]
        else:
            extent = [
                signal_text2,
            # f"止损：{zhisun}({zhisunR}%)\n\n",
        ]
        # 25小时内的新信号 就可以 进行钉钉推送
        if pushSer.add_push_item(after_signal, instId,extent):
            logger.info(signal_text)

    # 完成消息的通知
pushSer.push_message()

